package com.ljf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljf.annotation.HasPermission;
import com.ljf.annotation.NoRequireLogin;
import com.ljf.dto.EmpPageDto;
import com.ljf.entity.Employee;
import com.ljf.service.EmployeeService;
import com.ljf.util.PathUtils;
import com.ljf.util.Result;
import com.ljf.vo.DetailEmpVo;
import com.ljf.vo.EditEmpVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/personnel_management/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/update")
    public Result updateEmp(Employee employee, HttpServletRequest request){
        String token = request.getHeader("token");
        employeeService.updateEmp(employee,token);
        return Result.success();
    }

    @HasPermission("personnel_management:employee:list")
    @PostMapping("/list")
    public Result pageList(EmpPageDto empPageDto){
        Page<Employee> page = employeeService.pageEmp(empPageDto);
        return Result.success(page);
    }

    @HasPermission("personnel_management:employee:list")
    @GetMapping("/detail")
    public Result detail(Long uid){
        DetailEmpVo vo = employeeService.detail(uid);
        return Result.success(vo);
    }

    /**
     * 修改得按钮，点击后展示员工信息
     * @param uid
     * @return
     */
    @GetMapping("/editbtn")
    public Result editbtn(Long uid){
        EditEmpVo vo = employeeService.editbtn(uid);
        return Result.success(vo);
    }

    /**
     * 重置密码
     * @param eid
     * @param code
     * @return
     */
    @PostMapping("/resetPwd")
    public Result resetPwd(Long eid,String code){
        employeeService.resetPwd(eid,code);
        return Result.success();
    }

    /**
     * 删除员工
     * @return
     */
    @PostMapping("/deactivate")
    public Result deacticate(Long id){
        employeeService.deactivate(id);
        return Result.success();
    }
    @PostMapping("/save")
    public Result savaEmp(Employee employee,HttpServletRequest request){
        String token = request.getHeader("token");
        employeeService.saveEmp(employee,token);
        return Result.success();
    }

    @NoRequireLogin
    @PostMapping("/uploadImg")
    public Map<String, Object> uploadImg(@RequestParam("file") MultipartFile upload) {
        Map<String, Object> map = new HashMap<>();
        if (upload != null && upload.getSize() > 0) {
            String path;
            try {
                path = PathUtils.upload(upload);
                map.put("uploaded", 1);  //成功
                map.put("url", path);  //成功
            } catch (Exception e) {
                e.printStackTrace();
                map.put("uploaded", 0);  //失败
                Map<String, Object> errorMap = new HashMap<>();
                errorMap.put("message", e.getMessage());
                map.put("error", errorMap);
            }
        } else {
            map.put("uploaded", 0);  //失败
            Map<String, Object> errorMap = new HashMap<>();
            errorMap.put("message", "上传失败，图片文件异常");
            map.put("error", errorMap);
        }
        return map;
    }
    @NoRequireLogin
    @PostMapping("/downloadImg")
    public Result downloadImg(@RequestParam MultipartFile img) throws IOException {
        String originalFilename = img.getOriginalFilename();
        byte[] bytes = img.getBytes();
        InputStream inputStream = img.getInputStream();
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\newImg\\"+originalFilename);
        BeanUtils.copyProperties(inputStream,fileOutputStream);
        fileOutputStream.write(bytes);
        fileOutputStream.close();
        inputStream.close();
        return Result.success();
    }
}
